﻿using System.Globalization;
using System.Windows.Controls;

namespace DimensionsHelper.Client.ValidationRules;

public class IntegerValidationRule : ValidationRule
{
    public bool Positive { get; set; }

    public bool AllowZero { get; set; }

    public override ValidationResult Validate(object? value, CultureInfo cultureInfo)
    {
        int? integer = value switch
        {
            int v => v,
            string str when int.TryParse(str, out int i) => i,
            _ => null
        };

        if (integer == null)
        {
            return new ValidationResult(false, "需要填写整数");
        }

        if (!AllowZero && integer == 0)
        {
            return new ValidationResult(false, "不能填写0");
        }

        if (Positive && integer < 0)
        {
            return new ValidationResult(false, "需要正整数");
        }

        return ValidationResult.ValidResult;
    }
}